package com.synjones.handsetS8.ild.net.api;

import com.synjones.handsetS8.ild.net.common.NetCallback;
import com.synjones.handsetS8.ild.net.common.NetClient;
import com.synjones.handsetS8.ild.net.model.NetResult;
import com.synjones.handsetS8.ild.net.model.UploadImageResult;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;
import retrofit2.http.Query;

/**
 * 文件 接口
 */
public class FileApi {
    private static final String BASE_URL = "http://jiangong-manager.dev1.i-laodong.com/";
    private static IdCardService service = NetClient.getRetrofit(BASE_URL).create(IdCardService.class);

    /**
     * 上传图片
     *
     * @param filePath 本地上传文件路径
     * @param callback 请求完成后的回调
     */
    public static void uploadImage(String filePath, NetCallback<NetResult<UploadImageResult>> callback) {
        File file = new File(filePath);
        RequestBody requestBody = RequestBody.create(MediaType.parse("images/*"), file);
        MultipartBody.Part part = MultipartBody.Part.createFormData("file", filePath, requestBody);
        //远程服务器上文件的保存路径
        String savePath = "hand_set_app";

        Call<NetResult<UploadImageResult>> call = service.uploadImage(savePath, part);
        call.enqueue(callback);
    }

    interface IdCardService {
        /**
         * 上传图片
         *
         * @return
         */
        @Multipart
        @POST("api/upload/upload-image")
        Call<NetResult<UploadImageResult>> uploadImage(@Query("path") String path, @Part MultipartBody.Part part);
    }
}
